local tianjiang = fk.CreateSkill{
    name = "ym1__tianjiang",
}
Fk:loadTranslationTable{
    ["ym1__tianjiang"] = "天匠",
    [":ym1__tianjiang"] = "游戏开始时，将两张不同副类别的装备牌置入你的装备区。摸牌阶段，你可以检索一张指定副类别的装备，若成功，你获得并少摸一张牌。",
    ["#ym1__tianjiang-invoke"] = "天匠：你可以声明并获得一种副类别的装备",
}

tianjiang:addEffect(fk.GameStart, {
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(tianjiang.name)
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local equipMap = {}
        for _, id in ipairs(room.draw_pile) do
            local sub_type = Fk:getCardById(id).sub_type
            if Fk:getCardById(id).type == Card.TypeEquip and player:hasEmptyEquipSlot(sub_type) then
                local list = equipMap[tostring(sub_type)] or {}
                table.insert(list, id)
                equipMap[tostring(sub_type)] = list
            end
        end
        local types = {}
        for k, _ in pairs(equipMap) do
            table.insert(types, k)
        end
        if #types == 0 then return end
        types = table.random(types, 2)
        local put = {}
        for _, t in ipairs(types) do
            table.insert(put, table.random(equipMap[t]))
        end
        room:notifySkillInvoked(player,tianjiang.name,"drawcard")
        player:broadcastSkillInvoke(tianjiang.name,1)
        room:moveCardIntoEquip(player, put, tianjiang.name, false, player)
    end,
})

tianjiang:addEffect(fk.DrawNCards, {
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(tianjiang.name) and target == player and data.n > 0
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices = {"treasure", "weapon", "armor", "defensive_horse", "offensive_horse", "Cancel"}
        local choice = room:askToChoice(player, {
            choices = choices,
            skill_name = tianjiang.name,
            prompt = "#ym1__tianjiang-invoke"
        })
        if choice ~= "Cancel" then
            event:setCostData(self, choice)
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        room:notifySkillInvoked(player,tianjiang.name,"drawcard")
        player:broadcastSkillInvoke(tianjiang.name,table.random({2,3}))
        local StrToSubtypeList = {["weapon"]={Card.SubtypeWeapon},["armor"]={Card.SubtypeArmor},["treasure"]={Card.SubtypeTreasure},["offensive_horse"]={Card.SubtypeOffensiveRide},["defensive_horse"]={Card.SubtypeDefensiveRide}}
        local choice = event:getCostData(self)
        local list = StrToSubtypeList[choice]
        local cards = {}
        if math.random() > 0.3 then
            local piles = table.simpleClone(room.draw_pile)
            table.insertTable(piles, room.discard_pile)
            for _, id in ipairs(piles) do
                if table.contains(list, Fk:getCardById(id).sub_type) then
                    table.insert(cards, id)
                end
            end
        else
            local ingame = Fk:getAllCardNames("e",false,true)
            local banned = {"kingsbane","poisonous_dagger","crossbow"}
            for _, card in ipairs(Fk.cards) do
                local name = card.name
                if card and card.type == Card.TypeEquip and table.contains(list, card.sub_type) and not table.contains(banned,name) then
                    --print(Util.TranslateMapper(name))
                    if not table.find(ingame, function (ingamename)
                    return ingamename == name
                    end) then
                        if #table.filter(cards,function(cid)
                            local card1=Fk:getCardById(cid)
                            return card1.name == card.name
                        end) == 0 then
                            local c = room:printCard(name, card.suit, card.number)
                            room:setCardMark(c, MarkEnum.DestructIntoDiscard, 1)
                            table.insertIfNeed(cards, c.id)
                        end
                    end
                end
            end
        end
        if #cards == 0 then return end
        room:moveCardTo(table.random(cards), Card.PlayerHand, player, fk.ReasonPrey, tianjiang.name)
        data.n = data.n - 1
    end,
})

return tianjiang